package com.company.Thread;

import java.util.concurrent.*;

/**
 * @ProjectName: 复习java
 * @Package: com.company.Thread
 * @ClassName: MyThreadPoolDemo
 * @Author: wym
 * @Date: 2020/8/19 22:36
 * @Version: 1.0
 */
public class MyThreadPoolDemo {
    public static void main(String[] args) {
//        System.out.println(Runtime.getRuntime().availableProcessors());
////        //ExecutorService threadPool= Executors.newFixedThreadPool(5);
////        ExecutorService threadPool=Executors.newSingleThreadExecutor();
////      // ExecutorService executorService = Executors.newCachedThreadPool();
////
////        try{
////            for (int i = 0; i < 10; i++) {
////                threadPool.execute(()->{
////                    System.out.println(Thread.currentThread().getName()+"办理业务");
////                });
////            }
////
////        }catch (Exception e){
////            e.printStackTrace();
////        }finally {
////            System.out.println("test");
////        }
////
        System.out.println(Runtime.getRuntime().availableProcessors());
//        ExecutorService executorService1=Executors.newFixedThreadPool(5);
//        ExecutorService executorService2=Executors.newSingleThreadExecutor();
        //ExecutorService executorService3=Executors.newCachedThreadPool();
        //手动创建 7个参数的  这里 最大支持8个
        ExecutorService executorService= new ThreadPoolExecutor(
                2,
                5,
                1L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()//回退到调用者
               // new ThreadPoolExecutor.AbortPolicy()
        );
        try {
            for (int i = 1; i <= 10; i++) {
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName() + "\t 办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            executorService.shutdown();
        }

    }
}
